{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9f3a5450",
   "metadata": {},
   "source": [
    "# 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4c623d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "Hongen\n"
     ]
    }
   ],
   "source": [
    "names = \"Hongen , zhichao \"\n",
    "print(names[0])  #输出的只是字母\n",
    "# 列表\n",
    "names = [\"Hongen\" , \"zhichao\"]\n",
    "print(names[0])   #输出的名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3eec725f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zhichao', 'hongen']\n",
      "[1234, 3456]\n"
     ]
    }
   ],
   "source": [
    "# 举例\n",
    "user_list = [\"zhichao\" , \"hongen\"]\n",
    "password_list = [1234 , 3456]\n",
    "print(user_list)\n",
    "print(password_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7fdb8ed",
   "metadata": {},
   "source": [
    "## 列表的嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dbdd94ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhichao\n",
      "1234\n",
      "['marry', 2345]\n"
     ]
    }
   ],
   "source": [
    "user_list = [\"zhichao\" , \"hongen\"]\n",
    "password_list = [1234 , 3456]\n",
    "info_list = [\"hongen\" , 1234],[\"marry\" , 2345]\n",
    "print(user_list[0])\n",
    "print(password_list[0])\n",
    "print(info_list[-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "93bbfd3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# slice()切片，数值是指values在list中的位置\n",
    "# names = [\"Mike\",\"Marry\"]\n",
    "# print(names[-2,-1]) #从右往左取值，-1不取值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29332ed8",
   "metadata": {},
   "source": [
    "## 列表的基本方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "54e82bcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hongen', 'Mike', 'Marry', 'Xiaohong']\n"
     ]
    }
   ],
   "source": [
    "# 1、append()增加方法，一般增加末尾\n",
    "stu_id = [101,102,103]\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "names.append(\"Xiaohong\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "db570942",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hongen', 'Xiaohong', 'Mike', 'Marry']\n"
     ]
    }
   ],
   "source": [
    "# 2、insert（）插入方法。 insert(1，\"youge\"),分别是插入的指定序号和名字\n",
    "stu_id = [101,102,103]\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "names.insert(1,\"Xiaohong\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "df17ff0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hongen', 'Marry']\n",
      "[1002, 1003, 1001, 1004]\n",
      "['Hongen', 'Mike']\n"
     ]
    }
   ],
   "source": [
    "# 3、remove()和pop（）\n",
    "# 优势：如果删除list中的多个元素时，remove比较合适\n",
    "# 3.1、remove（）一般请况下，用remove先用in判断\n",
    "stu_id = [101,102,103]\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "names.remove(\"Mike\")\n",
    "print(names)\n",
    "# 删除全部相同的数字，用for循环\n",
    "stu_id = [1001,1002,1003,1001,1001,1001,1004]\n",
    "for i in stu_id:\n",
    "    if i == 1001:\n",
    "        stu_id.remove(i)\n",
    "print(stu_id)\n",
    "# 3.2、pop（）删除位置为1的这个元素‘\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "names.pop(-1) #负数从右往左，正数从左往右\n",
    "print(names)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "41ef9a5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "# 4、index（）索引，查看该元素如1001在stu_id中第一个出现的位置索引\n",
    "stu_id = [1001,1002,1003,1004]\n",
    "print(stu_id.index(1001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "989d355d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 5、clear（）清楚所有的数据\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "names.clear()\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8474e58c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hongen', 'Mike', 'Marry', 101, 102, 103]\n"
     ]
    }
   ],
   "source": [
    "# 6、extend（）给指定的list进行扩充\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "stu_id = [101,102,103]\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "3ce08a1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "# 7、count（）查看列表元素出现的个数，统计分数出现的次数\n",
    "stu_id = [101,102,103]\n",
    "print(stu_id.count(101))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "e2193e29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 8、len（）长度\n",
    "names = [\"Hongen\",\"Mike\",\"Marry\"]\n",
    "print(len(names))   #len()查看names的列表元素个数，in查看是否在list当中，False/True\n",
    "print(\"zhichao\" in names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65507abe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9、split（）用于列表的拆分\n",
    "#语法：str.split(\"\"),引号里为分隔符，拆分的符号和词语"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "14781350",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1001, 1002, 1003, 1005] [1001, 1002, 1003, 1005]\n",
      "[1001, 1002, 1003] [1001, 1002, 1003]\n",
      "[100, 1002, 1003, 1005, 1001]\n"
     ]
    }
   ],
   "source": [
    "# 列表的高级方法 copy()不等子赋道 \n",
    "#copy()\n",
    "stu_id = [1001,1002,1003,1005]\n",
    "stu_id_matedata=stu_id.copy() \n",
    "print(stu_id,stu_id_matedata)\n",
    "# 切片实现复制\n",
    "stu_id=[1001,1002,1003]\n",
    "stu_id_matedata=stu_id[:]\n",
    "print(stu_id,stu_id_matedata)\n",
    "#索引值\n",
    "stu_id=[100,1002,1003,1005,1001]\n",
    "for i in stu_id:\n",
    "    if i==1001:\n",
    "        print(stu_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94cc04b1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
